const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

// pages/check_stu_in/check_stu_in.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected_list:[]
  },dis(){},
  
  bindchange(e){
    this.setData({
      selected_list:e.detail.value
    })
  },

  // 批量同意/拒绝进入
  submit(e){
    let selected_list = this.data.selected_list
    if(selected_list.length==0){
      return
    }
    var jg_stu_id_list = []
    for(var i=0; i<selected_list.length; i++){
      jg_stu_id_list.push(this.data.stu_list[selected_list[i]].jg_stu_id)
    }
    // console.log('jg_stu_id_list',jg_stu_id_list)
    let status = e.currentTarget.id
    // console.log('status',status)
    wx.showModal({
      content: '确认批量' + (status==3?'【同意】':'【拒绝】') +'这些学员进入班级吗？',
      showCancel: true,
      title: '提示',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + 'check_stu_in_submit',
            data: {
              jg_stu_id_list:JSON.stringify(jg_stu_id_list),
              status:status,
            },...parameter,
            success: res => {
              if(res.data.finished){
                wx.showToast({
                  title: '操作完成！',
                })
                this.load_uncheck_stu(this.jg_id)
              }
            },
          })
        }
      },
    })
  }, 

 
  // 全选
  selected_all(e){
    var selected_list = this.data.selected_list
    var stu_list = this.data.stu_list
    for(var i=0; i<stu_list.length; i++){
      let index = selected_list.indexOf(i.toString())
      if(index>-1){
        selected_list.splice(index,1)
      }else{
        selected_list.push(i.toString())
      }
    }
    this.setData({
      selected_list:selected_list
    })
  },


  // 更新学员状态
  update_stu_status(index,set_status){
    let stu_list = this.data.stu_list
    let jg_stu_id = stu_list[index].jg_stu_id
    wx.request({
      url: domainName + 'check_stu_in_updateStuStatus',
      data: {
        jg_stu_id:jg_stu_id,
        set_status:set_status,
      },...parameter,
      success: res => {
        // console.log('res',res.data)
        if(res.data.update_res){
          wx.showToast({
            title: '操控完成！',
          })
          this.load_uncheck_stu(this.jg_id)
        }
      },
    })
  },

  // 进入班级，同意/拒绝
  enter_jg(e){
    let index = e.currentTarget.id
    let status = e.detail.value
    wx.showModal({
      content: '确定此操作吗？',
      showCancel: true,
      title: status==3?'同意！':status==2?'拒绝！':'',
      success: res => {
        if(res.confirm){
          this.update_stu_status(index, status)
        }else{
          this.setData({
            stu_list:this.data.stu_list
          })
        }
      },
    })
  },



  view_stu_info(e){
    getApp().get_vibration()
    let index = e.currentTarget.id
    let jg_stu_id = this.data.stu_list[index].jg_stu_id
    wx.navigateTo({
      url:'/pages/students/stu_register/stuRegister_info/stuRegister_info?jg_stu_id=' + jg_stu_id + '&page_index=' + index
    })
    // this.can_show = true
  },





  view_photo(e){
    getApp().get_vibration()
    let index = e.currentTarget.id
    let photo = this.data.stu_list[index].photo
    wx.previewImage({
      urls: [domainName+photo],
    })
  },

  load_uncheck_stu(jg_id){
    wx.request({
      url: domainName + 'check_stu_in_init',
      data: {
        jg_id:jg_id
      },...parameter,
      success: res => {
        // console.log('res',res.data)
        let stu_list = res.data.uncheck_stu
        wx.setNavigationBarTitle({
          title: '进班审核(' + stu_list.length + '人)',
        })
        this.setData({
          stu_list:stu_list,
          selected_list:[],
          no_more:stu_list.length==0?true:false,
        })
      },
    })
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log('check_stu_in_options',options)
    this.jg_id = options.jg_id
    this.load_uncheck_stu(this.jg_id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.can_show){
      this.load_uncheck_stu(this.jg_id)
      this.can_show = false
      // 刷新上一页的申请进班的列表
      
    }
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.load_uncheck_stu(this.jg_id)
    wx.stopPullDownRefresh()
  },

})